﻿using System;
using System.Windows;
using System.Windows.Forms;
using System.Threading;
using System.Collections.ObjectModel;

using SimpleMvvmToolkit;

namespace GUIEXAM13
{
    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// Use the <strong>mvvmprop</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// </summary>
    public class CreateClothingViewModel : ViewModelDetailBase<CreateClothingViewModel, ClothingDomain>, IViewModel
    {
        private IViewModelMediator _viewModelMediator;
        private Clothing ClothingModel;
        
        public CreateClothingViewModel(Clothing clothingModel)
        {
            ClothingModel = clothingModel;
        }

        public void OpenFileBrowser()
        {
            var fileDialog = new OpenFileDialog();
            fileDialog.InitialDirectory = Environment.CurrentDirectory;
            fileDialog.Filter = "JPG Images (*.jpg)|*.jpg|PNG Images (*.png)|*.png";
            //"Text files (*.txt)|*.txt|All files (*.*)|*.*"
            if (fileDialog.ShowDialog() == DialogResult.OK)
                SelectedClothingDomain.Picture = fileDialog.FileName;

            PopupOpen = true;
        }

        private ClothingDomain _selectedClothingDomain;
        public ClothingDomain SelectedClothingDomain
        {
            get { return _selectedClothingDomain ?? (_selectedClothingDomain = new ClothingDomain()); }
            set
            {
                _selectedClothingDomain = value;
                NotifyPropertyChanged(m => m.SelectedClothingDomain);
            }
        }

        private bool _popupOpen;
        public bool PopupOpen
        {
            get { return _popupOpen; }
            set
            {
                _popupOpen = value;
                NotifyPropertyChanged(m => m.PopupOpen);
            }
        }

        public void TogglePopup()
        {
            PopupOpen = !PopupOpen;
        }

        public void SaveClothing()
        {
            ClothingModel.AllClothing.Add(SelectedClothingDomain);
            ClothingModel.SaveAllClothing();
            PopupOpen = false;
            SelectedClothingDomain = new ClothingDomain();
        }

        // TODO: Add events to notify the view or obtain data from the view
        public event EventHandler<NotificationEventArgs<Exception>> ErrorNotice;
        
        // Helper method to notify View of an error
        private void NotifyError(string message, Exception error)
        {
            // Notify view of an error
            Notify(ErrorNotice, new NotificationEventArgs<Exception>(message, error));
        }


        //Mediator
        public string GetName()
        {
            return "CreateClothingViewModel";
        }

        public void Send(string receiver, ViewModelMessage message)
        {
            _viewModelMediator.Send(GetName(), receiver, message);
        }

        public void Receive(string from, ViewModelMessage message)
        {
            throw new NotImplementedException();
        }
    }
}